package org.jeecg.modules.api.wlhy.customer;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.wlhy.customer.controller.HyCustomerController;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerConfig;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerConfigService;
import org.jeecg.modules.wlhy.order.entity.HyOrder;
import org.jeecg.modules.wlhy.order.service.IHyOrderService;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: APP货主配置
 * @author: luruirui
 * @date: 2021年05月22日 15:24
 */
@Api(tags="APP货主配置")
@RestController
@RequestMapping("/api/wlhy/hyCustomerConfig")
@Slf4j
public class ApiCustomerConfigController extends JeecgController<HyCustomerConfig, IHyCustomerConfigService> {

    @Autowired
    private IHyCustomerConfigService hyCustomerConfigService;

    @Autowired
    private IHyOrderService hyOrderService;

    @Autowired
    private UserUtil userUtil;


    /**
     *   获取config
     *
     * @return
     */
//    @AutoLog(value = "司机获取APP货主配置-司机获取APP货主配置")
    @ApiOperation(value="司机获取APP货主配置-司机获取APP货主配置", notes="司机获取APP货主配置-司机获取APP货主配置")
    @GetMapping(value = "/driverGetInfoForCustomer")
    public Result<?> driverGetInfoForCustomer(@RequestParam(name="orderId") String orderId) {

        HyOrder order = hyOrderService.getById(orderId);

        if(order==null){
            return Result.error("未找到货源信息");
        }
        HyCustomerConfig hyCustomerConfig = hyCustomerConfigService.getByCustomerId(order.getCustomerId());
        if(hyCustomerConfig==null) {
            return Result.error("未找到货主配置信息");
        }
        return Result.OK(hyCustomerConfig);
    }
    /**
     *   获取config
     *
     * @return
     */
//    @AutoLog(value = "APP货主配置-货主APP获取")
    @ApiOperation(value="APP货主配置-货主APP获取", notes="APP货主配置-货主APP获取")
    @GetMapping(value = "/getInfoForCustomer")
    public Result<?> getInfoForCustomer() {
        HyCustomer customer = userUtil.getCustomer();
        if(customer==null){
            return Result.error("未找到货主信息");
        }
        HyCustomerConfig hyCustomerConfig = hyCustomerConfigService.getByCustomerId(customer.getId());
        if(hyCustomerConfig==null) {
            return Result.error("未找到货主配置信息");
        }
        return Result.OK(hyCustomerConfig);
    }

    /**
     *   获取config
     *
     * @return
     */
//    @AutoLog(value = "APP货主配置-司机APP获取")
    @ApiOperation(value="APP货主配置-司机APP获取", notes="APP货主配置-司机APP获取")
    @GetMapping(value = "/getInfoForDriver")
    public Result<?> getInfoForDriver(@RequestParam(name="customerId") String customerId) {
        if(customerId==null || customerId.equals("")){
            return Result.error("货主ID错误！");
        }
        HyCustomerConfig hyCustomerConfig = hyCustomerConfigService.getByCustomerId(customerId);
        if(hyCustomerConfig==null) {
            return Result.error("未找到货主配置信息");
        }
        return Result.OK(hyCustomerConfig);
    }

    /**
     *  编辑
     *
     * @param hyCustomerConfig
     * @return
     */
    @AutoLog(value = "APP货主配置-编辑")
    @ApiOperation(value="APP货主配置-编辑", notes="APP货主配置-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody HyCustomerConfig hyCustomerConfig) {
        HyCustomer customer = userUtil.getCustomer();
        if(customer==null){
            return Result.error("未找到货主信息");
        }
        if(!StringUtils.equals(customer.getId(),hyCustomerConfig.getCustomerId())){
            return Result.error("货主信息错误");
        }
        hyCustomerConfigService.edit(hyCustomerConfig);
        return Result.OK("编辑成功!");
    }
}
